/* 
    获取一个职位在不同地区的信息，得到的是.html页面
    再提取html页面中的详情链接，得到的是链接组成的.txt文件
*/

// @ts-nocheck
const { resolve, dirname } = require('path');
const fs = require('fs');
const { translate } = require(resolve('./lib/translate/index.js'));
const { getJobUrl } = require('./getInfo.js');
const { getHref } = require('./getHref.js');
const env = require(resolve('./config.js'));
const dp = env[env.mode];

getJobUrl().then(({ res, key }) => {
    let allArea = Promise.all(res);
    allArea.then(
        async(value) => {
            console.log('数据获取完毕！');
            let filename = await translate(key);
            let baseUrl = resolve(`./out/${filename.trans_result.data[0].dst}/`);
            if (!fs.existsSync(baseUrl)) {
                fs.mkdirSync(baseUrl);
            }
            value.forEach(async(item, index) => {
                let url = resolve(`./out/${filename.trans_result.data[0].dst}/${filename.trans_result.data[0].dst}_${dp[index].code}.html`);
                var fd = fs.openSync(url, "w");
                fs.writeSync(fd, item.data);
                console.log(`★ ${dp[index].city}——${key}职位信息写入成功，在out文件夹中查看★`);
                if (index == dp.length) {
                    fs.closeSync(fd);
                }
                getHref(url, dp[index].city, key);
            })
        },
        reason => {
            console.log('数据请求失败！');
        }
    )
})